<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/21 0021
 * Time: 下午 1:26
 */
namespace Admin\Controller;
use Org\Util\Page;
class DivisionController extends CommonController
{
    public function _initialize()
    {
        parent::_initialize();
        $this->_mod = D('Division');
    }
    public function index (){
        $map = $this->_search();
        $mod = D($this->_name);
        !empty($mod) && $this->_list($mod, $map);
        $this->options = $this->check();
        $big_menu = array(
            'title' => '添加分组',
            'iframe' => U('Division/add'),
            'id' => 'add',
            'width' => '500',
            'height' => '360'
        );
        $this->assign('big_menu', $big_menu);
        $this->display();
    }
    protected function _list($model, $map = array(), $pagesize=20){

        //如果需要分页
        $count = $model->where($map)->count('id');
        $pager = new Page($count, $pagesize);
        $page = $pager->show();
        $this->assign("page", $page);

        $list = $model->get_list ($map, $pager->firstRow, $pager->listRows);
        $this->assign('list', $list);
        $this->assign('list_table', true);
    }
    protected function _search() {
        $map = array();
        ($time_start = I('get.time_start')) && $map['add_time'][] = array('egt', strtotime($time_start));
        ($time_end = I('get.time_end')) && $map['add_time'][] = array('elt', strtotime($time_end)+(24*60*60-1));
        ($keyword = I('get.keyword')) && $map['title'] = array('like', '%'.$keyword.'%');
        ($shop_id = I('get.shop_id','')) && $map['shop_id'] = array('eq', $shop_id);

        if( $_GET['status']==null ){
            $status = -1;
        }else{
            $status = intval($_GET['status']);
        }
        $status>=0 && $map['status'] = array('eq',$status);

        $this->assign('search', array(
            'time_start' => $time_start,
            'time_end' => $time_end,
            'shop_id' => $shop_id,
            'status'  => $status,
            'keyword' => $keyword
        ));
        return $map;
    }
    protected function _before_insert($data) {
        $shop_title = D('shop')->where(array('id'=>$data['shop_id']))->getField('title');
        $data['shop_title'] = $shop_title;
        $data['add_time']   = time();
        return $this->_insertPic($data);
    }
    public function ajax_get_group (){
        $shop_id = I ('post.shop_id', '');
        $groups = $this->_mod->get_groups_in_shop ($shop_id);
        exit (json_encode($groups));
    }


}